﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Adams.Public.DataAccess.Web.Home
{
    public static class Index
    {
        public static Adams.Public.DataTransfer.Web.Home.Index.PageLoad PageLoad()
        {
            var data = new Adams.Public.DataTransfer.Web.Home.Index.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SET NOCOUNT ON

SELECT TOP 5 O.*, E.CalendarID, C.Color
FROM Events.Occurances O
INNER JOIN Events.Events E ON O.EventID = E.EventID
INNER JOIN Events.Calendars C ON C.CalendarID = E.CalendarID
WHERE DATEDIFF(d, GETDATE(), O.StartDate) >= 0
ORDER BY O.StartDate

SELECT A.*
FROM Announcements.Announcements A
WHERE DATEDIFF(d, GetDate(), A.EndDate) > 0 OR DATEDIFF(d, GetDate(), A.StartDate) > 0

SELECT TOP 3 P.*
FROM PressReleases.PressReleases P
ORDER BY P.ReleaseDate DESC

DECLARE @LocationID int
SELECT @LocationID = LocationID
FROM DailyPrayers.Locations 
WHERE IsMaster = 1

SELECT * 
FROM DailyPrayers.PrayerTimes 
WHERE LocationID = @LocationID AND Month = DATEPART(m, GETDATE()) AND Day = DATEPART(d, GETDATE())

SELECT * FROM 
JummahPrayers.Locations 
ORDER BY DisplayOrder

SELECT * 
FROM JummahPrayers.Prayers
ORDER BY DisplayOrder

SELECT * FROM 
TarawehPrayers.Locations 
ORDER BY DisplayOrder

SELECT * 
FROM TarawehPrayers.Prayers
ORDER BY DisplayOrder

SELECT * FROM 
EidPrayers.Locations 
ORDER BY DisplayOrder

SELECT * 
FROM EidPrayers.Prayers
ORDER BY DisplayOrder
", CommandType.Text))
            {
                data.Events = new Adams.Shared.Entities.Events.OccuranceCollection();
                while (dr.Read())
                {
                    var evet = new Adams.Shared.Entities.Events.Occurance();
                    evet.ID = dr["OccuranceID"].DbInt();
                    evet.StartDate = dr["StartDate"].DbDate();
                    evet.EndDate = dr["EndDate"].DbDate();
                    evet.Title = dr["Title"].DbString();
                    evet.ShortDescription = dr["ShortDescription"].DbString();
                    evet.IsAllDay = dr["IsAllDay"].DbBool();
                    evet.Event = new Shared.Entities.Events.Event()
                    {
                        ID = dr["EventID"].DbInt(),
                        Calendar = new Shared.Entities.Events.Calendar() {
                            ID = dr["CalendarID"].DbInt(),
                            Color = dr["Color"].DbString()
                        }
                    };
                    data.Events.Add(evet);
                }

                dr.NextResult();
                data.Announcements = new Adams.Shared.Entities.Announcements.AnnouncementCollection();
                while (dr.Read())
                {
                    var announcement = new Adams.Shared.Entities.Announcements.Announcement();
                    announcement.ID = dr["AnnouncementID"].DbInt();
                    announcement.Title = dr["Title"].DbString();
                    announcement.ShortDescription = dr["ShortDescription"].DbString();
                    announcement.StartDate = dr["StartDate"].DbDate();
                    announcement.EndDate = dr["EndDate"].DbDate();
                    announcement.Link = dr["Link"].DbString();
                    data.Announcements.Add(announcement);
                }

                dr.NextResult();
                data.PressReleases = new Adams.Shared.Entities.PressReleases.PressReleaseCollection();
                while (dr.Read())
                {
                    var pressrelease = new Adams.Shared.Entities.PressReleases.PressRelease();
                    pressrelease.ID = dr["PressReleaseID"].DbInt();
                    pressrelease.Title = dr["Title"].DbString();
                    pressrelease.ShortDescription = dr["ShortDescription"].DbString();
                    pressrelease.ReleaseDate = dr["ReleaseDate"].DbDate();
                    data.PressReleases.Add(pressrelease);
                }

                dr.NextResult();
                data.PrayerTime = new Adams.Shared.Entities.DailyPrayers.PrayerTime();
                while (dr.Read())
                {
                    data.PrayerTime.ID = dr["PrayerID"].DbInt();
                    data.PrayerTime.Month = dr["Month"].DbInt();
                    data.PrayerTime.Day = dr["Day"].DbInt();
                    data.PrayerTime.Fajr = dr["Fajr"].DbString();
                    data.PrayerTime.FajrIqama = dr["FajrIqama"].DbString();
                    data.PrayerTime.Sunrise = dr["Sunrise"].DbString();
                    data.PrayerTime.Duhr = dr["Duhr"].DbString();
                    data.PrayerTime.DuhrIqama = dr["DuhrIqama"].DbString();
                    data.PrayerTime.Asr = dr["Asr"].DbString();
                    data.PrayerTime.AsrIqama = dr["AsrIqama"].DbString();
                    data.PrayerTime.Maghrib = dr["Maghrib"].DbString();
                    data.PrayerTime.MaghribIqama = dr["MaghribIqama"].DbString();
                    data.PrayerTime.Isha = dr["Isha"].DbString();
                    data.PrayerTime.IshaIqama = dr["IshaIqama"].DbString();
                }

                dr.NextResult();
                data.JummahLocations = new Adams.Shared.Entities.JummahPrayers.LocationCollection();
                while (dr.Read())
                {
                    var jummahlocation = new Adams.Shared.Entities.JummahPrayers.Location()
                    {
                        Prayers = new Adams.Shared.Entities.JummahPrayers.PrayerCollection()
                    };
                    jummahlocation.ID = dr["LocationID"].DbInt();
                    jummahlocation.Name = dr["Name"].DbString();
                    jummahlocation.FridayNote = dr["FridayNote"].DbString();
                    data.JummahLocations.Add(jummahlocation);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    int locationid = dr["LocationID"].DbInt();
                    var jprayer = new Adams.Shared.Entities.JummahPrayers.Prayer();
                    jprayer.ID = dr["PrayerID"].DbInt();
                    jprayer.Name = dr["Name"].DbString();
                    jprayer.Time = dr["Time"].DbString();

                    data.JummahLocations.Where(l => l.ID == locationid).FirstOrDefault().Prayers.Add(jprayer);
                }

                dr.NextResult();
                data.TarawehLocations = new Shared.Entities.TarawehPrayers.LocationCollection();
                while (dr.Read())
                {
                    var tlocation = new Adams.Shared.Entities.TarawehPrayers.Location()
                    {
                        Prayers = new Adams.Shared.Entities.TarawehPrayers.PrayerCollection()
                    };
                    tlocation.ID = dr["LocationID"].DbInt();
                    tlocation.Name = dr["Name"].DbString();
                    data.TarawehLocations.Add(tlocation);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    int locationid = dr["LocationID"].DbInt();
                    var jprayer = new Adams.Shared.Entities.TarawehPrayers.Prayer();
                    jprayer.ID = dr["PrayerID"].DbInt();
                    jprayer.Name = dr["Name"].DbString();
                    jprayer.Time = dr["Time"].DbString();

                    data.TarawehLocations.Where(l => l.ID == locationid).FirstOrDefault().Prayers.Add(jprayer);
                }

                dr.NextResult();
                data.EidLocations = new Shared.Entities.EidPrayers.LocationCollection();
                while (dr.Read())
                {
                    var elocation = new Adams.Shared.Entities.EidPrayers.Location()
                    {
                        Prayers = new Adams.Shared.Entities.EidPrayers.PrayerCollection()
                    };
                    elocation.ID = dr["LocationID"].DbInt();
                    elocation.Name = dr["Name"].DbString();
                    data.EidLocations.Add(elocation);
                }

                dr.NextResult();
                while (dr.Read())
                {
                    int locationid = dr["LocationID"].DbInt();
                    var jprayer = new Adams.Shared.Entities.EidPrayers.Prayer();
                    jprayer.ID = dr["PrayerID"].DbInt();
                    jprayer.Name = dr["Name"].DbString();
                    jprayer.Time = dr["Time"].DbString();

                    data.EidLocations.Where(l => l.ID == locationid).FirstOrDefault().Prayers.Add(jprayer);
                }

                return data;
            }
        }
    }
}